package com.chainyoung.common.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chainyoung.common.base.SimpleActivity;
import com.chainyoung.common.common_widget.loadsir.callback.Callback;
import com.chainyoung.common.common_widget.loadsir.core.LoadService;
import com.chainyoung.common.common_widget.loadsir.core.LoadSir;
import com.chainyoung.common.custom_widget.EmptyCallback;
import com.chainyoung.common.custom_widget.LoadingCallback;
import com.chainyoung.common.custom_widget.RetryCallback;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.mvp.IView;
import com.chainyoung.common.utils.ToastUtil;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class SimpleFragment extends Fragment implements IView {

    protected View mView;
    protected SimpleActivity mActivity;
    protected Context mContext;
    protected LoadService mLoadService;
    private Unbinder mUnBinder;



    protected boolean mIsPrepared = false;
    protected boolean mIsFirstResume = true;
    protected boolean mIsFirstVisible = true;
    protected boolean mIsFirstInvisible = true;




    @Override
    public void onAttach(Context context) {
        mActivity = (SimpleActivity) context;
        mContext = context;
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(getLayoutId(), null);
        if (useLoadSir()) {
            mLoadService = LoadSir.getDefault().register(mView, new Callback.OnReloadListener() {
                @Override
                public void onReload(View v) {
                    onPageRetry(v);
                }
            });
            return mLoadService.getLoadLayout();
        } else {
            return mView;
        }
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mUnBinder = ButterKnife.bind(this, view);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initUi();
        initData();
        initPrepare();

    }

    protected abstract void initData();

    protected abstract void initUi();

    public synchronized void initPrepare() {
        if (mIsPrepared) {
            onFirstUserVisible();
        } else {
            mIsPrepared = true;
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            if (mIsFirstVisible) {
                mIsFirstVisible = false;
                initPrepare();
            } else {
                onUserVisible();
            }
        } else {
            if (mIsFirstInvisible) {
                mIsFirstInvisible = false;
                onFirstUserInvisible();
            } else {
                onUserInvisible();
            }
        }
    }

    protected AppComponent getAppComponent() {
        return mActivity.mAppComponent;
    }


    /**
     * 第一次对用户可见时会调用该方法
     */
    protected abstract void onFirstUserVisible();


    /**
     * 对用户可见时会调用该方法，除了第一次
     */
    public void onUserVisible() {
    }

    /**
     * 第一次对用户不可见时会调用该方法
     */
    public void onFirstUserInvisible() {
    }


    /**
     * 对用户不可见时会调用该方法，除了第一次
     */
    public void onUserInvisible() {
    }


    /**
     * 获取当前Fragment状态
     *
     * @return true为正常 false为未加载或正在删除
     */
    private boolean getStatus() {
        return (isAdded() && !isRemoving());
    }

    /**
     * 启动方法
     */
    protected void launch(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(mActivity, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        mActivity.startActivity(intent);
    }

    // startActivity
    public void launch(Class<?> cls) {
        launch(cls, null);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnBinder.unbind();
    }


    /**
     * 页面重试
     */
    protected void onPageRetry(View v) {

    }

    protected abstract int getLayoutId();

    /**
     * loadsir注册目标，默认为自身acitivity
     */
    protected Object registerTarget() {
        return this;
    }

    /**
     * 是否使用loadsir，默认不使用
     */
    protected boolean useLoadSir() {
        return false;
    }


    @Override
    public void onResume() {
        super.onResume();
        if (mIsFirstResume) {
            mIsFirstResume = false;
            return;
        }
        if (getUserVisibleHint()) {
            onUserVisible();
        }
    }


    @Override
    public void onPause() {
        super.onPause();
        // MobclickAgent.onPageEnd("Fragment");
        if (getUserVisibleHint()) {
            onUserInvisible();
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        //  EventBus.getDefault().unregister(this);
        //unbinder.unbind();

    }

    @Override
    public void showLoading(String content) {
        if (getStatus()) {
            if (mActivity != null && !mActivity.isFinishing()) {
                mActivity.showLoading(content);
            }
        }

    }

    @Override
    public void hideLoading() {
        if (getStatus()) {
            if (mActivity != null) {
                mActivity.hideLoading();
            }
        }
    }

    @Override
    public void showError(String message) {
        hideLoading();
        ToastUtil.showToast(message);
    }

    @Override
    public void showError(int stringId) {
        showError(getString(stringId));

    }

    @Override
    public void showMsg(String message) {
        ToastUtil.showToast(message);
    }

    @Override
    public void showMsg(int stringId) {
        showMsg(getString(stringId));
    }

    @Override
    public void showPageLoading() {
        if (mLoadService != null) {
            mLoadService.showCallback(LoadingCallback.class);
        }
    }

    @Override
    public void showPageEmpty() {
        if (mLoadService != null) {
            mLoadService.showCallback(EmptyCallback.class);
        }
    }

    @Override
    public void showPageError() {
        if (mLoadService != null) {
            mLoadService.showCallback(RetryCallback.class);
        }
    }

    @Override
    public void showPageContent() {
        if (mLoadService != null) {
            mLoadService.showSuccess();
        }
    }


}
